<?php
    class Page_messages extends V_View {
    	function index () {
    		$this->assign('page_title','Сообщения');
    		$this->display('header.tpl');
            $this->display('pages/messages/index.tpl');
            $this->display('footer.tpl');
        }
        function getmessagelist () {
            // Получение сообщений
        	$message=V_DB::fetchAll(
        		'SELECT us.name, us.login, ms.message, ms.target_user_id, ms.id FROM v_message_list AS ms '.
        		'LEFT JOIN v_user_list AS us ON ms.send_user_id=us.id '.
        		'WHERE ms.target_user_id='.V_User::getId().' AND ms.is_del_target=0'.
        		' OR ms.send_user_id='.V_User::getId().' AND ms.is_del_send=0'.
        		' ORDER BY ms.id DESC');
            if (!empty($message)) {
            	$messages[$i]['author']=$message['name'];
            	$messages[$i]['author_login']=$message['login'];
            	$messages[$i]['message']=$message['message'];
            	$messages[$i]['id']=$message['id'];
            	if ($message['target_user_id']==V_User::getId()) $messages[$i]['target']='in';
            	else $messages[$i]['target']='out';
            }
            if (isset($messages)) $this->assign ('messages',$messages);
            $this->display('pages/messages/getmessage.tpl');
        }
    	function getmessage () {
            // Получение сообщений
        	$message=V_DB::fetch('SELECT message FROM v_message_list WHERE id='.V_DB::escape(V_Route::getId()));
            if (!empty($message)) {
            	echo $message['message'];
            }
        }
        function write () {
            // Если форма отправлена - проверка данных
            if (isset ($_POST['action']) and $_POST['action']=='write') {
            	$state="OK";
            	if (trim($_POST['message']) == '')
                    $state="INPUT";
                if (trim($_POST['target']) == '')
                    $state="INPUT";
                else {
                	$result=V_DB::query('SELECT id FROM v_user_list WHERE login="'.V_DB::escape($_POST['target']).'"');
                	if ($result and V_DB::num_rows($result)==1) {
                		$t_user=V_DB::fetch_array($result);
                		$t_user_id=$t_user['id'];
                	} else $state="TARGETERROR";
                }
                if ($state=="OK") {
                	if (V_DB::insert('v_message_list',Array(
                		Array ('name' => 'send_user_id','value' => '"'.V_User::getId().'"'),
                		Array ('name' => 'target_user_id','value' => '"'.$t_user_id.'"'),
                		Array ('name' => 'message','value' => '"'.V_DB::escape($_POST['message']).'"'),
                	))) echo "OK";
                	else echo "ERROR";
                } else echo $state;
            }
        }
        function count () {
            // Получение сообщений
        	echo V_DB::count('v_message_list','target_user_id='.V_User::getId().' AND is_del_target=0');
        }
    	function delete () {
            $router = V_Engine::Create('Router');
            $user = V_Engine::Create('User');
            $result=V_DB::query('SELECT send_user_id, target_user_id FROM v_message_list '.
        						'WHERE id='.V_DB::escape($router->getId()));
            $message=V_DB::fetch_array($result);
            if ($message['target_user_id']==V_User::getId())
            	V_DB::query('UPDATE v_message_list SET is_del_target=1 WHERE id='.
            				V_DB::escape($router->getId()));
           	if ($message['send_user_id']==V_User::getId())
            	V_DB::query('UPDATE v_message_list SET is_del_send=1 WHERE id='.
            				V_DB::escape($router->getId()));
            $result=V_DB::query('SELECT is_del_target, is_del_send FROM v_message_list '.
        						'WHERE id='.V_DB::escape($router->getId()));
            $message=V_DB::fetch_array($result);
            if ($message['is_del_target'] && $message['is_del_send'])
            	V_DB::query('DELETE FROM v_message_list'.' WHERE id='.V_DB::escape($router->getId()));
            echo 'OK';
        }
    }